package com.ruoyi.kiku.service.impl;

import java.util.List;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.kiku.mapper.OfficialWebsiteBannerInfoMapper;
import com.ruoyi.kiku.domain.OfficialWebsiteBannerInfo;
import com.ruoyi.kiku.service.IOfficialWebsiteBannerInfoService;

/**
 * 轮播信息Service业务层处理
 *
 * @author TT
 * @date 2025-08-25
 */
@Service
public class OfficialWebsiteBannerInfoServiceImpl implements IOfficialWebsiteBannerInfoService
{
    @Autowired
    private OfficialWebsiteBannerInfoMapper officialWebsiteBannerInfoMapper;
    @Autowired
    private RedisCache redisCache;

    /**
     * 查询轮播信息
     *
     * @param id 轮播信息主键
     * @return 轮播信息
     */
    @Override
    public OfficialWebsiteBannerInfo selectOfficialWebsiteBannerInfoById(Long id)
    {
        return officialWebsiteBannerInfoMapper.selectOfficialWebsiteBannerInfoById(id);
    }

    /**
     * 查询轮播信息列表
     *
     * @param officialWebsiteBannerInfo 轮播信息
     * @return 轮播信息
     */
    @Override
    public List<OfficialWebsiteBannerInfo> selectOfficialWebsiteBannerInfoList(OfficialWebsiteBannerInfo officialWebsiteBannerInfo)
    {
        return officialWebsiteBannerInfoMapper.selectOfficialWebsiteBannerInfoList(officialWebsiteBannerInfo);
    }

    /**
     * 查询轮播信息列表(redis 缓存)
     *
     * @return 轮播信息
     */
    @Override
    public List<OfficialWebsiteBannerInfo> selectOfficialWebsiteBannerInfoListFromRedis()
    {
        List<OfficialWebsiteBannerInfo> cacheBanners = officialWebsiteBannerInfoMapper.selectOfficialWebsiteBannerInfoList(new OfficialWebsiteBannerInfo());
        return cacheBanners;
    }

    /**
     * 新增轮播信息
     *
     * @param officialWebsiteBannerInfo 轮播信息
     * @return 结果
     */
    @Override
    public int insertOfficialWebsiteBannerInfo(OfficialWebsiteBannerInfo officialWebsiteBannerInfo)
    {
        officialWebsiteBannerInfo.setCreateTime(DateUtils.getNowDate());
        int i = officialWebsiteBannerInfoMapper.insertOfficialWebsiteBannerInfo(officialWebsiteBannerInfo);
        return i;
    }

    /**
     * 修改轮播信息
     *
     * @param officialWebsiteBannerInfo 轮播信息
     * @return 结果
     */
    @Override
    public int updateOfficialWebsiteBannerInfo(OfficialWebsiteBannerInfo officialWebsiteBannerInfo)
    {
        officialWebsiteBannerInfo.setUpdateTime(DateUtils.getNowDate());
        int i = officialWebsiteBannerInfoMapper.updateOfficialWebsiteBannerInfo(officialWebsiteBannerInfo);
        return i;
    }

    /**
     * 批量删除轮播信息
     *
     * @param ids 需要删除的轮播信息主键
     * @return 结果
     */
    @Override
    public int deleteOfficialWebsiteBannerInfoByIds(Long[] ids)
    {
        int i = officialWebsiteBannerInfoMapper.deleteOfficialWebsiteBannerInfoByIds(ids);
        return i;
    }

    /**
     * 删除轮播信息信息
     *
     * @param id 轮播信息主键
     * @return 结果
     */
    @Override
    public int deleteOfficialWebsiteBannerInfoById(Long id)
    {
        int i = officialWebsiteBannerInfoMapper.deleteOfficialWebsiteBannerInfoById(id);
        return i;
    }
    /**
     * 轮播信息添加缓存最新五条，缓存一个月
     */
    public List<OfficialWebsiteBannerInfo> officialWebSiteBannerAddRedis(){
        List<OfficialWebsiteBannerInfo> list
                = officialWebsiteBannerInfoMapper.selectOfficialWebsiteBannerInfoList(new OfficialWebsiteBannerInfo());
        // 提取前面五条
        if (list.size() > 5){
            list = list.subList(0, 5);
        }
        redisCache.setCacheList("officialWebSite:bannerList", list);
        // 缓存一个月
        redisCache.expire("officialWebSite:bannerList", 30 * 24 * 60 * 60);
        return list;
    }

}
